<?php

namespace App\library\services\lib\aliyun;

use App\library\services\ConfigService;
use OSS\Core\OssException;
use OSS\OssClient;

class Oss
{
    private $accessKeyId;
    private $accessKeySecret;
    private $endpoint;
    private $bucket;

    /**
     * Oss constructor.
     * 初始化配置
     */
    public function __construct()
    {
        $this->accessKeyId = ConfigService::getValue('aliyun_access_key_id');
        $this->accessKeySecret = ConfigService::getValue('aliyun_access_key_secret');
        $this->endpoint = ConfigService::getValue('aliyun_alioss_endpoint');
        $this->bucket= ConfigService::getValue('aliyun_alioss_bucket');
    }

    /**
     * Author:Zhongyuan Lu
     * @param $fileName
     * @param $filePath
     * @return array
     */
    public function upload($fileName,$filePath)
    {
        try{
            $ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint);

            $res = $ossClient->uploadFile($this->bucket, $fileName, $filePath);
        } catch(OssException $e) {
            return ['code' => 400, 'data' => [], 'message' => $e->getMessage()];
        }
        return ['code' => 200, 'data' => $res, 'message' => '上传成功'];
    }
}
